const fs = require('fs')
const path = require('path')
const Dir = path.resolve(__dirname, '../src/api') // 需要读取文件的绝对路径
// 读取文件夹，第二个参数表示读取内容为对象形式
const files = fs.readdirSync(Dir, { withFileTypes: true })
let str = ''
const strMap = []
const promiseAll = []

for (const file of files) {
  // 文件
  if (!file.isFile()) continue
  const promise = new Promise((resolve, reject) => {
    fs.readFile(Dir + `/${file.name}`, 'utf8', function (err, content) {
      if (err) return reject(err)
      content.split('\n').forEach(c => {
        const reg = /(.*)url:.*['"](.*)['"]/g
        let match
        while ((match = reg.exec(c)) !== null) {
          // 判断是否被注释
          if (!/\/\//.test(match[1])) {
            const url = match[2].split('?')[0]
            if (!strMap.includes(url)) {
              str += url + '\n'
              strMap.push(url)
            }
          }
        }
      })
      resolve()
    })
  })
  promiseAll.push(promise)
}
Promise.all(promiseAll).then(res => {
  fs.writeFile('url.txt', str, (err) => {
    if (err) throw err
    console.log('File created!')
  })
})
